package com.mydemo.controller;

import com.mydemo.records.StudentRecord;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.function.Consumer;

@RestController
public class StructuredOutputController {

    /**
     * 通过 chatmodel 实现流式响应
     */
    @Resource(name="deepseek")
    private ChatModel deepSeekModel;

    @Resource(name="qwen")
    private ChatModel qwenModel;

    /**
     * 通过 chatclient 实现流式响应
     */
    @Resource(name="deepSeekChatClient")
    private ChatClient deepSeekClient;
    @Resource(name="qwenChatClient")
    private ChatClient qwenClient;


    @GetMapping(value = "/structuredOutput1")
    public StudentRecord chat1(@RequestParam(name="sname") String sname,
                              @RequestParam(name="email") String email){

        return qwenClient.prompt().user(new Consumer<ChatClient.PromptUserSpec>() {
            @Override
            public void accept(ChatClient.PromptUserSpec promptUserSpec) {
                promptUserSpec.text("学号1001，我叫{sanme}，大学专业电子信息工程，我的邮箱{email}")
                        .param("sanme",sname)
                        .param("email",email);
            }
        }).call().entity(StudentRecord.class);
    }


    @GetMapping(value = "/structuredOutput2")
    public StudentRecord chat2(@RequestParam(name="sname") String sname,
                               @RequestParam(name="email") String email){

        String stringTemplate = """
            学号1002，我叫{sanme}，大学专业电子信息工程，我的邮箱{email}
            """;

        return qwenClient.prompt().user(promptUserSpec -> promptUserSpec.text(stringTemplate)
                .param("sanme",sname)
                .param("email",email))
                .call()
                .entity(StudentRecord.class);
    }
}
